package com.mervin.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/***************************************
 * @author: mervin
 * @Date: 2020-05-04 14:29
 * @Description:
 ***************************************/
public class ReflectUtil {

    public static List<Field> getAllDeclaredFields(Class<?> clazz){
        List<Field> fieldList = new ArrayList<>();
        //向上循环  遍历父类
        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
            Field[] fields = clazz.getDeclaredFields();
            fieldList.addAll(Arrays.asList(fields));
        }
        return fieldList;
    }

    public static boolean isAnnotationPresent(Class<?> clazz, Class<? extends Annotation> annotationClass){
        return clazz.isAnnotationPresent(annotationClass);
    }

    public static boolean isAnnotationPresent(Field field, Class<? extends Annotation> annotationClass){
        return field.isAnnotationPresent(annotationClass);
    }

}
